//
//  QDImportMessageView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/26.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 重要信息的视图
final class QDImportantMessageView: NWPopWindow {

    /// 设置顶部的标题
    @objc var title: String = "" {
        willSet {
            titleLabel.text = newValue
        }
    }
    
    /// 设置敬语的标题
    @objc var subtitle: String = "" {
        willSet {
            topLabel.text = newValue
        }
    }
    
    /// 设置公告详细内容
    @objc var detailTitle: String = "" {
        willSet {
            if newValue == detailTitle { return }
            attributeDetailTitle = newValue
        }
    }
    
    /// 设置属性字符串
    private var attributeDetailTitle: String = "" {
        willSet {
            
            // 如果是空，不展示属性字符串
            if newValue.isEmpty { return }

            //段落属性
            let style = NSMutableParagraphStyle()
            style.alignment = .justified
            style.firstLineHeadIndent = 30
            style.maximumLineHeight = 22
            
            //生成属性字符串
            let attributeString = NSAttributedString(string: newValue, attributes: [.paragraphStyle : style,
                                                                                    .font: Font.regular.font(size: 16),
                                                                                    .foregroundColor: QDColors.black])
            
            detailLabel.attributedText = attributeString
        }
    }
    
    /// 容器视图,放置渐变色的视图
    private let containerView = UIView()
    /// 显示的重要标题
    private let titleLabel = UILabel()
    /// 左侧的横线
    private let leftLineView = UIView()
    /// 右侧的横线
    private let rightLineView = UIView()
    /// 文本的容器的视图
    private let textContainerView = UIView()
    /// 顶部的标签
    private let topLabel = UILabel()
    /// 详细的内容
    private let detailLabel = UILabel()
    /// icon的imageView
    private let iconImageView = UIImageView()
    /// 关闭按钮
    private let closeImageView = UIImageView()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //移除背景响应
        if let backgroundControl = backgroundView.subviews.filter({ $0 is UIControl }).first {
            backgroundControl.removeFromSuperview()
        }
        
        containerView.layer.cornerRadius = 8
        containerView.clipsToBounds = true
        //追加渐变layer
        let gradLayer = CAGradientLayer()
        gradLayer.bounds = CGRect(origin: .zero, size: CGSize(width: 310, height: 384))
        gradLayer.colors = [#colorLiteral(red: 0.4039215686, green: 0.5019607843, blue: 0.8588235294, alpha: 1).cgColor,#colorLiteral(red: 0.4274509804, green: 0.7215686275, blue: 0.9490196078, alpha: 1).cgColor]
        gradLayer.anchorPoint = .zero
        gradLayer.startPoint = CGPoint(x: 0.5, y: 0)
        gradLayer.endPoint = CGPoint(x: 0.5, y: 1)
        containerView.layer.insertSublayer(gradLayer, at: 0)
        
        leftLineView.backgroundColor = .white
        rightLineView.backgroundColor = .white
        
        titleLabel.font = Font.medium.font(size: 22)
        titleLabel.textColor = .white
        titleLabel.textAlignment = .center
        titleLabel.text = "重要信息公告"
        
        textContainerView.layer.cornerRadius = 8
        textContainerView.backgroundColor = #colorLiteral(red: 0.9803921569, green: 0.9843137255, blue: 1, alpha: 1)
        
        topLabel.textColor = QDColors.black
        topLabel.font = Font.regular.font(size: 16)
        topLabel.text = "尊敬的旅客您好："
        
        detailLabel.numberOfLines = 0
        
        iconImageView.backgroundColor = #colorLiteral(red: 0.9803921569, green: 0.9843137255, blue: 1, alpha: 1)
        iconImageView.image = "imp_logo".namedImage
        
        closeImageView.backgroundColor = .clear
        closeImageView.image = "imp_close".namedImage
        closeImageView.addUIControlHandlerTarget(self, action: #selector(dismissAnimated))
        
        contentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        contentView.addSubview(containerView)
        contentView.addSubview(closeImageView)
        
        containerView.addSubview(titleLabel)
        containerView.addSubview(leftLineView)
        containerView.addSubview(rightLineView)
        containerView.addSubview(textContainerView)
        
        textContainerView.addSubview(topLabel)
        textContainerView.addSubview(detailLabel)
        textContainerView.addSubview(iconImageView)
        
        
        containerView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-22.5)
            make.width.equalTo(310)
            make.height.equalTo(384)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(30)
            make.centerX.equalToSuperview()
            make.width.lessThanOrEqualTo(132)
            make.top.equalToSuperview().offset(14)
        }
        
        leftLineView.snp.makeConstraints { (make) in
            make.height.equalTo(0.5)
            make.width.equalTo(36)
            make.centerY.equalTo(titleLabel)
            make.right.equalTo(titleLabel.snp.left).offset(-10)
        }
        
        rightLineView.snp.makeConstraints { (make) in
            make.height.equalTo(0.5)
            make.width.equalTo(36)
            make.centerY.equalTo(titleLabel)
            make.left.equalTo(titleLabel.snp.right).offset(10)
        }
        
        textContainerView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().inset(10)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.bottom.equalToSuperview().inset(14)
        }
        
        topLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().inset(16)
            make.height.equalTo(22)
            make.top.equalToSuperview().offset(16)
        }
        
        detailLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().inset(15)
            make.top.equalTo(topLabel.snp.bottom).offset(8)
//            make.bottom.equalToSuperview().inset(70)
            make.height.lessThanOrEqualTo(198)
        }
        
        iconImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(23)
            make.width.equalTo(85)
            make.height.equalTo(34)
            make.bottom.equalToSuperview().inset(12)
        }
        
        closeImageView.snp.makeConstraints { (make) in
            make.top.equalTo(containerView.snp.bottom).offset(28)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(36)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 显示QDImportantMessageView
    ///
    /// - Parameters:
    ///   - title: 顶部的标题
    ///   - topTitle: 敬语的文字
    ///   - detail: 详细内容
    static func show(title: String?,topTitle: String?, detail: String?){
        sharedInstance().title = title ?? ""
        sharedInstance().subtitle = topTitle ?? ""
        sharedInstance().detailTitle = detail ?? ""
        sharedInstance().show()
    }
    
    
    override func dismissAnimated() {
        removeFromSuperview()
    }
}
